import publicForm from "@/components/public/el-form.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项
import {
    saveClNumberingrule, //保存
    getClNumberingruleById, //详情


} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",

    ],
    data() {
        return {
            visible: "",
            loading: false,
            title: null,
            isLock: false,
            btnStatus: [],
            fNote: formJs.fNote,
            dialogHeight: null,
            activeIndex: "1",
            formObj: formJs.formObj,
            currntData: null,
            dataInfo: null,
            formData: {
                fId: "", //id
                fRulename: "", //规则名称
                fCreatedates: "", //添加日期
                fCreateusername: "", //添加人员
                fSerialnumber: "", //开始序号
                fCompanyid: "", //单位id
                fCompanyname: "", //单位
                fNumberingrule: "CLJC-{BD}-{yyyy}{MM}{dd}-{0000}", //编号规则
                fNote: "", //规则描述
                userId: "", //当前登录账户ID
                userName: "", //当前登录账户名
                fNote1: "",
            },
            btnShow: true,
            currentDetail: null,

        };
    },
    components: {
        publicForm,
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },

        statusNum: {
            handler(newVal) {
                if (newVal) {
                    this.edit(newVal);
                }
            },
            immediate: true,
        },

        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentDetail = newVal;
                    if (this.statusNum != 1) {
                        this.getDetail({
                            fId: this.currentDetail.fId
                        }) // currentRow
                    }
                }
            },
            immediate: true,
        },

    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("GxBack-userInfo"));
        this.btnShow = true

        if (this.statusNum == 1) { //编辑
            this.formData = this.$options.data.call(this).formData;
            this.formData.fId = this.currentDetail.fId //按钮 
            this.getDetail({
                fId: this.currentDetail.fId
            })
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.btnShow = true
        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            this.formObj.map((item) => {
                item.disabled = true;
            });
        }
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.76 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },

        edit() {
            this.formData = this.$options.data.call(this).formData;
        },
        // 详情
        getDetail(obj) {
            this.loading = true;
            getClNumberingruleById(obj).then((res) => {
                if (res.errcode == 0) {
                    this.loading = false;
                    var data = res.data.yjQmpClNumberingrule
                    this.formData = data
                    this.formData.fNote1 =
                        "1、可手动更改编号规则，更改成功后“编号示例”实时展示一个示例效果;2、编号规则中只能输入数字、字母、短横线和英文的()[]{}；3、{BD}代表标段号、{yyyy}代表4位年数字（如2019）、{yy}代表2位年数字（如19）、{MM}代表月数字（如12）、{dd}代表天数字（如31）；4、{0000}代表4为序号数字（如0001），此为必须项！"
                } else {
                    this.loading = false;
                    this.$message.error(res.msg);
                }
            });
        },
        yj_saveForm() {
            this.formData.userId = this.userInfo.userId; //当前登录人id
            this.formData.userName = this.userInfo.realName; //当前登录人名称
            this.formData.fCreateusername = this.userInfo.realName; //添加人员
            this.formData.fCompanyid = this.currentDetail.fCompanyid; //单位id
            this.formData.fCompanyname = this.currentDetail.fCompanyname; //单位

            // 0连续新增1新增
            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData);
            } else {
                console.log("error");
            }

        },
        // 保存
        saveForm(data) {

            this.isLock = true
            this.loading = true;

            saveClNumberingrule(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;

                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("新增成功");
                        this.isLock = false
                    } else {
                        this.loading = false;

                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })

        },
    },
};